﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_DAL
{
    /// <summary>
    /// 单元信息
    /// create by Guoph at 2017.3.24
    /// </summary>
    public class Zor_UnitInformation_DAL<T> where T : class   
    {
        /// <summary>
        /// 角色管理-修改
        /// </summary>
        /// <param name="unitInformation">单元信息对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer UpdateUnitInformation(Zor_UnitInfor unitInformation)
        {
            var sql = "";
            if (unitInformation.Uim_Id != 0)
                sql += "if not exists(select BD_Id,Uim_Name from [Zor_UnitInfor] where BD_Id=@Bd_Id and Uim_Name=@Uim_Name ) ";

            sql += "update [Zor_UnitInfor] set BD_Id=@BD_Id ,Uim_Name=@Uim_Name,Uim_BuildStructure=@Uim_BuildStructure  where Uim_Id = @Uim_Id";

            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, unitInformation );
            return bar;
        }


        /// <summary>
        /// 角色管理-新增
        /// </summary>
        /// <param name="unitInformation">单元信息对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer InsertUnitInformation(Zor_UnitInfor unitInformation)
        {
            var sql = @"if not exists(select BD_Id,Uim_Name from [Zor_UnitInfor] where BD_Id=@Bd_Id and Uim_Name=@Uim_Name  ) 
                        insert into [Zor_UnitInfor] values 
                        (@Bd_Id,@Uim_Name,@Uim_BuildStructure,@Uim_CreateDate,@User_Id,@Uim_Flage)";

            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, new
            {
                Bd_Id = unitInformation.BD_Id,                            //楼宇ID
                Uim_Name = unitInformation.Uim_Name,                      //单元名称
                Uim_BuildStructure = unitInformation.Uim_BuildStructure , //房屋结构
                Uim_Flage = unitInformation.Uim_Flage,
                Uim_CreateDate = TimeHelper.ToTimeStamp(DateTime.Now),    //创建时间
                User_Id = RequestSession.GetSessionUser().User_Id                            //当前登录用户
               
            });
            return bar;
        }
        /// <summary>
        /// 删除单元信息
        /// </summary>
        /// <param name="Uim_Id"></param>
        /// <returns>单元信息ID</returns>
        public static BaseAnswer DeleteUnitInformation(int[] Uim_Id)
        {

            var sql = @"if not exists(select Hs_Id from [Zor_Hous] where Uf_Id in (" + string.Join(",", Uim_Id).TrimEnd(',') + ")) delete from [Zor_UnitInfor]  where  Uim_Id in (" + string.Join(",", Uim_Id).TrimEnd(',') + ")";

            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Uim_Id = Uim_Id.ToArray<int>() });
            return bar;
        }

        /// <summary>
        /// 获取单元下拉列表
        /// </summary>
        /// <returns></returns>
        public static List<T> GetBuildingList()
        {
            var sql = "select Bd_Id,Bd_Name from dbo.Zor_Building";
            List<T> listBuilding = DapperQuery<T>.DoQueryList(sql, null);
            return listBuilding;
        }


        /// <summary>
        /// 获取所属街道名称
        /// </summary>
        /// <returns></returns>
        public static List<T> GetBdName()
        {
            var sql = "select Bd_Id,Bd_Name from dbo.Zor_Building where Bd_Id in (select  Bd_Id from Zor_Building where Dt_Id =" + RequestSession.GetSessionUser().DT_Id + " and Bd_Flage=1)";
            List<T> listDtName = DapperQuery<T>.DoQueryList(sql, null);
            return listDtName;

        }

        /// <summary>
        /// 获取单元信息列表（不分页）
        /// create 刘云飞 by 2017/04/06   
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectViewInformation(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1" + where;
            }
            var sql = "select * from [V_Zor_UnitInformation] where Uim_Flage = 2 and Dt_Id = " + RequestSession.GetSessionUser().DT_Id + " order by Uim_Id " + strwhere;
            return DapperQuery<T>.DoQueryList(sql, null);
        }
        /// <summary>
        /// 获取单元信息列表（不分页）
        /// create light by 2017/04/06   
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectViewDoorpInformation(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1 and " + where;
            }
            var sql = "select * from [V_Zor_UnitInformation] where " + strwhere + " and User_Id = " + RequestSession.GetSessionUser().User_Id + " order by Uim_Id " ;
            return DapperQuery<T>.DoQueryList(sql, null);
        }


    }
}
